package com.lier.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.lier.gulimall.product.entity.*;
import com.lier.gulimall.product.service.*;
import com.lier.gulimall.product.vo.SkuItemSaleAttrVo;
import com.lier.gulimall.product.vo.SkuItemVo;
import com.lier.gulimall.product.vo.SpuItemBaseAttrGroupVo;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lier.common.utils.PageUtils;
import com.lier.common.utils.Query;
import com.lier.gulimall.product.dao.SkuInfoDao;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Resource
    private SkuInfoService skuInfoService;
    @Resource
    private SkuImagesService skuImagesService;
    @Resource
    private SpuInfoDescService spuInfoDescService;
    @Resource
    private AttrGroupService attrGroupService;
    @Resource
    private SkuSaleAttrValueService skuSaleAttrValueService;
    @Resource
    private ThreadPoolExecutor threadPoolExecutor;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuInfo(SkuInfoEntity skuInfoEntity) {
        baseMapper.insert(skuInfoEntity);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {

        LambdaQueryWrapper<SkuInfoEntity> queryWrapper = new LambdaQueryWrapper<>();

        String key = (String)params.get("key");
        if(!StringUtils.isEmpty(key)){

            queryWrapper.and(wrapper -> {
                wrapper.eq(SkuInfoEntity::getSkuId, key).or().like(SkuInfoEntity::getSkuName, key);
            });
        }

        String brandId = (String)params.get("brandId");
        if(!"0".equals(brandId) && !StringUtils.isEmpty(brandId)){
            queryWrapper.eq(SkuInfoEntity::getBrandId, brandId);
        }

        String catelogId = (String)params.get("catelogId");
        if(!"0".equals(catelogId) && !StringUtils.isEmpty(catelogId)){
            queryWrapper.eq(SkuInfoEntity::getCatalogId, catelogId);
        }

        String min = (String)params.get("min");
        if(!StringUtils.isEmpty(min)){
            queryWrapper.ge(SkuInfoEntity::getPrice, min);
        }

        String max = (String)params.get("max");
        if(!StringUtils.isEmpty(max) && new BigDecimal(max).compareTo(new BigDecimal(0)) > 0){
            queryWrapper.le(SkuInfoEntity::getPrice, max);
        }

        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params), queryWrapper);

        return new PageUtils(page);
    }

    @Override
    public List<SkuInfoEntity> getSkusBuSpuId(Long spuId) {

        LambdaUpdateWrapper<SkuInfoEntity> skuInfoEntityLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        skuInfoEntityLambdaUpdateWrapper.eq(SkuInfoEntity::getSpuId, spuId);
        return baseMapper.selectList(skuInfoEntityLambdaUpdateWrapper);
    }

    @Override
    public SkuItemVo item(Long skuId) {
        SkuItemVo skuItemVo = new SkuItemVo();

        // sku基本信息 pms_sku_info
        CompletableFuture<SkuInfoEntity> infoFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfoEntity skuInfoEntity = skuInfoService.getById(skuId);
            skuItemVo.setInfo(skuInfoEntity);
            return skuInfoEntity;
        }, threadPoolExecutor);

        // spu得销售属性组合
        CompletableFuture<Void> saleAttrFuture = infoFuture.thenAcceptAsync(skuInfoEntity -> {
            List<SkuItemSaleAttrVo> skuItemSaleAttrVos = skuSaleAttrValueService.getSaleAttrsBySpuId(skuInfoEntity.getSpuId());
            skuItemVo.setSaleAttr(skuItemSaleAttrVos);
        }, threadPoolExecutor);

        // spu的介绍
        CompletableFuture<Void> spuDescFuture = infoFuture.thenAcceptAsync(info -> {
            SpuInfoDescEntity spuInfoDescEntity = spuInfoDescService.selectBySpuId(info.getSpuId());
            skuItemVo.setDesp(spuInfoDescEntity);
        }, threadPoolExecutor);

        // 规格参数信息
        CompletableFuture<Void> attrFuture = infoFuture.thenAcceptAsync(info -> {
            List<SpuItemBaseAttrGroupVo> groupAttrs = attrGroupService
                    .getAttrGroupWithAttrsBySpuId(info.getSpuId(), info.getCatalogId());
            skuItemVo.setGroupAttrs(groupAttrs);
        }, threadPoolExecutor);


        // sku图片信息 pms_sku_images
        CompletableFuture<Void> imagesFuture = CompletableFuture.runAsync(() -> {
            List<SkuImagesEntity> skuImagesEntityList = skuImagesService.selectAllImagesBySkuId(skuId);
            skuItemVo.setImages(skuImagesEntityList);
        }, threadPoolExecutor);


        // 等待上述所有异步任务完成
        CompletableFuture.allOf(saleAttrFuture, spuDescFuture, attrFuture, imagesFuture).join();

        return skuItemVo;
    }

}
